package com.github.gin.springboot.security;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.io.Serializable;

public class UserRealm extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    	Principal principal = (Principal)principals.getPrimaryPrincipal();

        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //authorizationInfo.setRoles(userService.getRoles(principal.getLoginName()));
        //authorizationInfo.setStringPermissions(userService.getPermissions(principal.getLoginName()));
        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

    	LoginNamePasswordToken myToken = (LoginNamePasswordToken)token;

        /*User user = userService.getByLoginName(myToken.getLoginName());
        
        if(user == null) {
            throw new UnknownAccountException();//没找到帐号
        }

        if(!Boolean.TRUE.equals(user.isValidate())){
            throw new LockedAccountException(); //账号未审核
        }

        if(!Boolean.TRUE.equals(user.isAvailable())) {
            throw new DisabledAccountException(); //帐号不可用
        }*/

        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
        		/*new Principal(user.getLoginName(),user.getName()),
                user.getPassword(), 
                ByteSource.Util.bytes(user.getSalt()),
                getName() */
        );
 
        return authenticationInfo;
    }

    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    public void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }
    
    public static class Principal implements Serializable {
    	
		private static final long serialVersionUID = 6933698998109354583L;
    	
    	private String loginName;
    	
    	private String name;
    	
    	public Principal(String loginName, String name){
    		this.loginName = loginName;
    		this.name = name;
    	}

		public String getLoginName() {
			return loginName;
		}

		public void setLoginName(String loginName) {
			this.loginName = loginName;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
    }

}

